Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalNumber: 0,
        totalPrice: 0,
        isAll: false,
        cartList: [
            {
            created_at: "2016-12-26 11:52:25",
            decr_class: "disabled",
            deleted_at: null,
            goods_attr: "口味:甜; 尺寸:L; ",
            goods_attr_ids: "1,3",
            goods_id: 1,
            goods_name: "大水果",
            goods_number: 1,
            goods_sn: "",
            id: 329,
            is_real: true,
            is_sale: true,
            market_price: "6.00",
            plus_class: "",
            real_price: "5.00",
            rec_type: 0,
            shop_price: 5,
            sku_id: 6,
            status: false,
            stock_num: 8,
            sum_price: 5,
            suppliers_id: 0,
            thumb_url: "http://img13.360buyimg.com/n1/jfs/t3628/1/307365085/338515/e3c727cb/5805b5d9N8a6dfb9b.jpg",
            unsale_reason: "",
            updated_at: "2017-02-21 17:23:45",
            user_id: 1835
        }, {
            created_at: "2016-12-26 14:42:37",
            decr_class: "disabled",
            deleted_at: null,
            goods_attr: "口味:甜; 重量:500g; ",
            goods_attr_ids: "1,6",
            goods_id: 6,
            goods_name: "奇异果",
            goods_number: 1,
            goods_sn: "",
            id: 330,
            is_real: true,
            is_sale: true,
            market_price: "212.00",
            plus_class: "disabled",
            real_price: "0.01",
            rec_type: 0,
            shop_price: 0.01,
            sku_id: 30,
            status: true,
            stock_num: 1,
            sum_price: 0.01,
            suppliers_id: 0,
            thumb_url: "https://img13.360buyimg.com/n1/jfs/t3655/151/1971152678/274261/fb508f8c/5840d683Na2190edd.jpg",
            unsale_reason: "",
            updated_at: "2017-02-21 17:23:45",
            user_id: 1835
        }]
    },
    //修改商品数量
    changeCount(event) {
        var index = event.currentTarget.dataset.index;
        var _num = event.currentTarget.dataset.num;
        if (this.data.cartList[index].goods_number == 1 && _num == -1) {
            return wx.showModal({
                title: '您选择的商品数量不能少于1',
            })
        }
        console.log(index);
        // let _num = this.cartList[index].goods_number+1;
        this.setData({
            [`cartList[${index}].goods_number`]: this.data.cartList[index].goods_number + _num
        });
        this.updatePriceAndCount();

    },
    //选择商品
    selectProduct(e) {
        // 当前选择的商品下标
        let _index = e.target.dataset.index;
        // 当前商品状态
        let _status = e.target.dataset.status;
        // 修改选中状态
        this.setData({
            [`cartList[${_index}].status`]: !_status
        });

        // 判断是否是全选
        this.isAll();
        // 重新计算数量和价格
        this.updatePriceAndCount();

    },
    // 全选功能
    allSelect() {
        // 修改全选状态
        this.setData({
            isAll: !this.data.isAll
        });
        let _cartList = this.data.cartList.map(item => {
            item.status = this.data.isAll;
            return item;
        });
        this.setData({
            cartList: _cartList
        });
        // 重新计算数量和价格
        this.updatePriceAndCount();
    },
    del(e) {
        let _id = e.currentTarget.dataset.id;
        console.log(e,'11111');
        console.log('_id', _id);
        let _list = this.data.cartList.filter(item => {
            return item.goods_id !== _id;
        });
        console.log(_list,'剩余--');

        this.setData({
            cartList: _list
        });
        this.updatePriceAndCount();

    },
    //计算总价格和总数量
    updatePriceAndCount() {
        let _cartList = this.data.cartList.filter(item => {
            return item.status
        });
        //   console.log(_cartList);
        let _sumPrice = 0;
        let _sumCount = 0;
        for (let i = 0; i < _cartList.length; i++) {
            _sumCount += _cartList[i].goods_number;
            _sumPrice += _cartList[i].real_price * _cartList[i].goods_number;
        }
        this.setData({
            totalPrice: _sumPrice,
            totalNumber: _sumCount
        });



    },
    //判断是否是全选 
    isAll() {
        let _isAll = this.data.cartList.every(item => {
            return item.status;
        });
        this.setData({
            isAll: _isAll
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 判断是否是全选
        this.isAll();
        // 重新计算数量和价格
        this.updatePriceAndCount();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})